/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.zeos.bean;

import br.zeos.ZDataSource;
import br.zeos.ZSql;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

/**
 *
 * @author hardware
 */
public class ZTableView extends JTable implements ActionListener {

    private ZDataSource dataSource = null;
    private String[] showFields = null;

    public void setDataSource(final ZDataSource dataSource) {
        this.dataSource = dataSource;
        this.dataSource.addUpdateListener(this);
        UpdateData();
    }

    public String[] getShowFields() {
        return showFields;
    }

    public void setShowFields(String[] showFields) {
        this.showFields = showFields;
    }

    private void UpdateData() {
        ZSql sql = this.dataSource.getSql();
        DefaultTableModel modelo = new DefaultTableModel() {

            @Override
            public boolean isCellEditable(int i, int i1) {
                return false;
            }

        };
        ArrayList<String> columns = new ArrayList<String>();
        if (this.showFields == null) {
            for (Map.Entry<String, ZSql.Tipos> sis1 : this.dataSource.getSql().getTabela().getFields().entrySet()) {
                String nome = sis1.getKey();
                ZSql.Tipos tipo = sis1.getValue();
                columns.add(nome);
            }
        } else {
            columns.addAll(Arrays.asList(this.showFields));//netbeans <3
        }
        for (String column : columns) {
            modelo.addColumn(column);

        }
        int i = 0;
        for (HashMap<String, String> e : this.dataSource.getData()) {
            modelo.addRow(new Object[]{});
            for (int j = 0; j < columns.size(); j++) {
                String porrada = columns.get(j).toUpperCase();
                String moscaCapeta = e.get(porrada);
                modelo.setValueAt(moscaCapeta, i, j);
            }
            i++;
        }

        selectIndex();
    }

    private void selectIndex() {
        if (this.getRowCount() > 0) {
            this.setRowSelectionInterval(this.dataSource.getIndex(), this.dataSource.getIndex());//é isso mesmo ?
        }
    }

    @Override
    public void actionPerformed(ActionEvent ae) {
        this.selectIndex();
        UpdateData();
        HashMap<String, String> linha = this.dataSource.getLine();
        if (this.getShowFields() == null) {
//            System.out.println("Um ponteiro nulo acertou meu cachorro que faleceu");
            return;
        }
        if (this.getShowFields().length < 1) {
            System.out.println("Bateu um vento no meu cachorro e ele bateu a cabeça na arvore");
            return;
        }
        System.out.println("meu cachorro saiu voando"); //VOU DEIXAR ESSA MENSAGEM POR QUE ELA ME MANTÉM COM SANIDADE :D
        String id = linha.get(this.getShowFields()[0].toUpperCase());
        for (int i = 0; i < this.getRowCount(); i++) {
            if (this.getValueAt(i, 0).toString().toUpperCase().contains(id.toUpperCase())) {
                this.setRowSelectionInterval(i, i);
            }
        }
    }

}
